import { request } from "../../request/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      { id: 0, value: "综合", isActive: true },
      { id: 1, value: "销量", isActive: false },
      { id: 2, value: "价格", isActive: false },
    ],
    goodsList: [],
  },
  // 商品数据
  queryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10
  },
  // 当前页码值
  totalPage: 1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryParams.cid = options.cid
    console.log(options)
    this.getGoodsList()
  },
  // 获取商品列表数据
  async getGoodsList() {
    let res = await request({ url: "/goods/search", data: this.queryParams })
    let total = res.total;
    console.log(res)
    this.totalPage = Math.ceil(total / this.queryParams.pagesize)
    console.log("total"+total)
    console.log(this.totalPage)
    this.setData({
      goodsList: [...this.data.goodsList, ...res.goods]
    })
    // 关闭下拉刷新的窗口
    wx.stopPullDownRefresh()
  },
  // 导航栏点击
  handleItemChange(e) {
    let { index } = e.detail
    let { tabs } = this.data
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
    this.setData({
      tabs
    })
  },
  // 页面上划 滚动条触底事件 继而获取更多数据
  onReachBottom() {
    if (this.queryParams.pagenum >= this.totalPage) {
      // console.log("没有")
      wx.showToast({
        title: '没有更多商品了',
        icon: "none"
      });

    } else {
      this.queryParams.pagenum++
      this.getGoodsList()
    }
  },
  // 监听用户下拉刷新
  onPullDownRefresh(){
    // 1. 重置数组
    this.setData({
      goodsList: []
    })
    // 2. 重置页码
    this.queryParams.pagenum = 1;
    // 3. 发送请求
    this.getGoodsList()
  }
})